package com.cyys.common.ql;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.HashMap;
import java.util.List;

public class JSONData {

	public static String toLayerTitle(HashMap<String,Object> map){
		if(map == null || map.size() == 0)
			return "[]";
		String buffer = "[";
		for(String key: map.keySet()){
			buffer += "{";
			buffer += "field:'" + key + "',";
			buffer += "title:'" + key + "',";
			buffer += "width:'120'";
			buffer += "},";
		}
		buffer = buffer.substring(0,buffer.lastIndexOf(","));
		buffer += "]";
		return buffer;
	}

	public static String toLayerData(IPage<?> page){
		StringBuffer buffer = new StringBuffer();
		buffer.append("{");
		if(page.getRecords() == null || page.getRecords().size() ==0){
			buffer.append("\"code\":1,\"msg\":\"没有数据。\",\"count\":0,\"data\":[]");
		}else{
			buffer.append("\"code\":0,");
			buffer.append("\"msg\":\"\",");
			buffer.append("\"count\":");
			buffer.append(page.getTotal());
			buffer.append(",");
			buffer.append("\"data\":");
			buffer.append(JSON.toJSONString(page.getRecords()));
		}
		buffer.append("}");
		return buffer.toString();
	}

	public static String toLayerDataNoPage(List<?> dataList){
		StringBuffer buffer = new StringBuffer();
		buffer.append("{");
		if(dataList == null || dataList.size() ==0){
			buffer.append("\"code\":1,\"msg\":\"没有数据。\",\"count\":0,\"data\":[]");
		}else{
			buffer.append("\"code\":0,");
			buffer.append("\"msg\":\"\",");
			buffer.append("\"count\":");
			buffer.append(dataList.size());
			buffer.append(",");
			buffer.append("\"data\":");
			buffer.append(JSON.toJSONString(dataList));
		}
		buffer.append("}");
		//JsonMapper.getInstance().toJson(buffer.toString());
		return buffer.toString();
	}
}
